/** copyright By @ kingboy **/
package com.csinc.generate.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.csinc.generate.core.base.annotion.BussinessLog;
import com.csinc.generate.core.base.annotion.Permission;
import com.csinc.generate.core.base.controller.BaseController;
import com.csinc.generate.core.common.factory.PageFactory;
import com.csinc.generate.core.exception.CsincException;
import com.csinc.generate.core.exception.CsincExceptionEnum;
import com.csinc.generate.core.utils.lang.StringUtils;
import com.csinc.generate.model.TSystemDictModel;
import com.csinc.generate.service.ITSystemDictService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 数据字典控制器
 *
 * @author kingboy
 * @Date 2018-08-01 17:24
 */
@Slf4j
@Controller
@RequestMapping("/tsystemdict")
public class TSystemDictController extends BaseController {

    private String PREFIX = "/biz/tsystemdict/";

    @Autowired
    private ITSystemDictService tSystemDictService;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "tsystemdict.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/goto_add")
    public String gotoAdd() {
        return PREFIX + "tsystemdict_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/goto_update/{id}")
    public String gotoUpdate(@PathVariable Integer id, Model model) {
        model.addAttribute("tsystemdict", tSystemDictService.selectById(id));
        return PREFIX + "tsystemdict_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(TSystemDictModel model,HttpServletRequest req, HttpServletResponse res) {
        Page<TSystemDictModel> page = new PageFactory<TSystemDictModel>().defaultPage();
//        page.setRecords(tSystemDictService.selectPage(page,model,new EntityWrapper<TSystemDictModel>()));
        return super.packForBT(tSystemDictService.selectPage(page,new EntityWrapper<TSystemDictModel>()));
    }

    @BussinessLog(value = "数据字典新增", key = "?" )
    @RequestMapping(value = "/add")
    @Permission
    @ResponseBody
    public Object add(TSystemDictModel model) {
        tSystemDictService.insert(model);
        return SUCCESS_TIP;
    }

    @BussinessLog(value = "数据字典删除", key = "id" )
    @RequestMapping(value = "/delete")
    @Permission
    @ResponseBody
    public Object delete(Integer id) {
        tSystemDictService.deleteById(id);
        return SUCCESS_TIP;
    }

    @BussinessLog(value = "数据字典修改", key = "id" )
    @RequestMapping(value = "/update")
    @Permission
    @ResponseBody
    public Object update(TSystemDictModel model) {
        if (StringUtils.isOneEmpty(model.getId())) {
        throw new CsincException(CsincExceptionEnum.REQUEST_NULL);
        }
        tSystemDictService.updateById(model);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/detail")
    @ResponseBody
    public TSystemDictModel detail(EntityWrapper<TSystemDictModel> model) {
        return tSystemDictService.selectOne(model);
    }
}